import time
import datetime
from remindmain import *
import requests
import json
import random

WEEKBEGIN=datetime.date(year=2021,month=3,day=1)
GROUP=258180904

def hitokoto():
    data={"type":random.choice(['a','b','c','d','e','f'])}
    result=requests.post('https://v1.alapi.cn/api/hitokoto',data=data)
    dict=json.loads(result.text)
    return (dict['data']['hitokoto'],dict['data']['from'])
def getStr(dict,timeStr):
    tip,source=hitokoto()
    str="上课提醒：\n" \
        "课程："+dict['班级']+"\n" \
        "时间："+timeStr+"\n" \
        "地点："+dict['地点']+"\n" \
        "Tip："+tip+"\n" \
        "-- "+source
    return str
def dealTime(timeStr):
    timeoDict={1:'10:00',
              2:'11:00',
              3:'12:10',
              4:'13:10',
              5:'14:10',
              6:'15:30',
              7:'16:30',
              8:'17:40',
              9:'18:40',
               10:'25:00',
               11:'25:00',
               12:'25:00',
               13:'25:00',
               14:'25:00'
              }
    timeDict={1:'9:50',
              2:'10:50',
              3:'12:00',
              4:'13:00',
              5:'14:00',
              6:'15:20',
              7:'16:20',
              8:'17:30',
              9:'18:30',
               10:'25:00',
               11:'25:00',
               12:'25:00',
               13:'25:00',
               14:'25:00'
              }
    timefDict={1:'10:50',
              2:'11:50',
              3:'13:00',
              4:'14:00',
              5:'14:40',
              6:'16:20',
              7:'17:20',
              8:'18:30',
              9:'19:20',
               10:'25:00',
               11:'25:00',
               12:'21:24',
               13:'25:00',
               14:'25:00'

              }
    timeBegin=int(timeStr.split('-')[0])
    timeEnd=int(timeStr.split('-')[1])
    return (timeDict[timeBegin],timeoDict[timeBegin]+' - '+timefDict[timeEnd])
def sendWord(bot:int,group:int,msg:str,anonymous:bool=False):
    header={'Content-Type':'application/json'}
    data={
    "bot":bot,
    "group": group,
    "msg":msg,
    "anonymous":anonymous
        }
    js = json.dumps(data)
    requests.post('http://129.211.74.183:53392/send_group_msg',data=js,headers=header)
calssData=XD_remind().deal_data()
while True:
    timeNow=time.time()
    weekBegin=WEEKBEGIN
    weekEnd=datetime.date.today()
    # 周次
    week=(weekEnd-weekBegin).days//7+1
    # 星期
    sunday = datetime.datetime.now().weekday() + 1
    # 小时:分钟
    hour=time.strftime('%H:%M',time.localtime(timeNow))
    for i in calssData:
        t,timeStr=dealTime(i['时间'])
        if week in i['周数'] and sunday==i['星期'] and t==hour:
            strInf=getStr(i,timeStr)
            sendWord(bot=2384945985,group=GROUP,msg=strInf)
            print(hour+'\n'+i)
            time.sleep(60)
    time.sleep(30)


